package com.feng.fengDUtil;


import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

public class MySqlUtil {

  public static String getIdListSql(List<String> ids) {
    String sql =
        " (SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(a.id,',',b.help_topic_id+1),',',-1) as id    from (select '"
            +
            StringUtils.join(ids, ",") +
            "' id) a left join mysql.help_topic b     on b.help_topic_id < (LENGTH(a.id)-LENGTH(REPLACE(a.id,',',''))+1) )";

    return sql;
  }

  private String escapeSQLLike(Object likeStr) {
    String str = StringUtils.replace(likeStr.toString(), "_", "\\_");
    str = StringUtils.replace(str, "%", "\\%");
    return str;
  }

  public String processLike(String str) {
    return "%" + escapeSQLLike(str) + "%";
  }

  public String endWith(Object str) {
    return "%" + escapeSQLLike(str);
  }

  public String startWith(Object str) {
    return escapeSQLLike(str) + "%";
  }

  public String genePlaceHolders(int length) {
    return " (" + String.join(",", Collections.nCopies(length, "?")) + ") ";
  }

  public void orderBy(StringBuilder sb, String script) {
    sb.append(" order by ").append(script);
  }

  public void addCondition(Object obj, String condition, StringBuilder sb, List params) {
    addCondition(obj, condition, sb, params, null);
  }

  public <T> void addCondition(T obj, String condition, StringBuilder sb, List params,
      Function<T, ? extends Object> function) {
    if (obj == null) {
      return;
    } else if (obj instanceof String && StringUtils.isNotEmpty((String) obj)) {
      sb.append(StringUtils.SPACE).append(condition);
      params.add(function == null ? obj : function.apply(obj));
    } else if (obj instanceof Collections && CollectionUtils.isNotEmpty((Collection) obj)) {
      Collection collection = (Collection) obj;
      sb.append(StringUtils.SPACE).append(condition).append(genePlaceHolders(collection.size()));
      params.addAll(collection);
    } else if (obj.getClass().isArray() && Array.getLength(obj) > 0) {
      Object[] array = (Object[]) obj;
      sb.append(StringUtils.SPACE).append(condition).append(genePlaceHolders(array.length));
      params.addAll(Arrays.asList(array));
    } else {
      sb.append(StringUtils.SPACE).append(condition);
      params.add(function == null ? obj : function.apply(obj));
    }
  }

  public static String geneInsertSql(String table, List<String> fieldList) {
    StringBuilder sb = new StringBuilder("insert into " + table);
    sb.append(" ( `").append(String.join("`,`", fieldList)).append("`) values ( ");
    sb.append(IntStream.range(0, fieldList.size()).mapToObj(n -> "?").collect(Collectors.joining(",")));
    sb.append(")");
    return sb.toString();
  }
}